//
//  LTQLoginViewController.swift
//  WeiBoProject
//
//  Created by MAC on 16/2/26.
//  Copyright © 2016年 LTQ. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD
class LTQLoginViewController: UIViewController,UIWebViewDelegate {
    
    var webView: UIWebView?
    
    override func loadView() {
        
        webView = UIWebView()
        //        设置代理   目的是拿到返回的request里面的token信息，
        webView?.delegate = self
        view = webView
        
        setupNav()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //        必须设置背景色
        webView?.backgroundColor = UIColor.whiteColor()
        
        //        设置请求的URL
        let url = NSURL(string: "https://api.weibo.com/oauth2/authorize?client_id=\(App_KEY)&redirect_uri=\(App_URL)&response_type=code")!
        
        //        创建一个请求
        let request = NSURLRequest(URL: url)
        
        //        加载请求
        webView?.loadRequest(request)
        
    }
    
    func setupNav (){
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: .Plain, target: self, action: "dismiss")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", style: .Plain, target: self, action: "auto")
        
    }
    
    func dismiss(){
        dismissViewControllerAnimated(true) { () -> Void in
            
        }
    }
    
    func auto(){
        
        let js = "document.getElementById(\"userId\").value = \"litianqi0101@163.com\";document.getElementById(\"passwd\").value = \"464635246\";"
        
        webView?.stringByEvaluatingJavaScriptFromString(js)
        
    }
}
extension LTQLoginViewController{
    
    func webViewDidStartLoad(webView: UIWebView) {
        SVProgressHUD.show()
    }
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
    }
    func webView(webView: UIWebView, didFailLoadWithError error: NSError?) {
        SVProgressHUD.dismiss()
    }
    
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        //        拿到全路径
        if let url = request.URL?.absoluteString{
            
            if url.hasPrefix(App_URL){
                
                if let code = request.URL?.query{
                    print(code)
                    
                    let subCode = code.substringFromIndex("code=".endIndex)
                    print(subCode)
                    
                    LTQUserViewModel.shareInstance.loadToken(subCode, success: { () -> () in
                        print("success")
                        
                        //                    发送通知，通知appdelegate去更改跟控制器
                        NSNotificationCenter.defaultCenter().postNotificationName(kSwitchRootViewControllerNotification, object: nil)
                        
                        //                    登陆成功当前的控制器需要销毁
                        self.dismissViewControllerAnimated(false, completion: { () -> Void in
                            print("控制器消失")
                            
                        })
                        }, failure: { () -> () in
                            print("failure")
                    })
                    //                    百度的界面就不会进行加载
                    return false
                }
                
            }
            
        }
        return true
        
    }
}
